#include "common.h"
#include "xtank.h"
//-----------------------------------------------------------------------------
XTank::XTank(XGame * pGame)
:XObject()
{
    game=pGame;
    printf("XTank -> Construktor\n");
    x=y=0;
}
//-----------------------------------------------------------------------------
XTank::~XTank()
{
    printf("XTank -> Destruktor\n");
}
//-----------------------------------------------------------------------------
void XTank::init(int posx,int posy)
{
    printf("XTank -> Init()\n");
    image=SDL_LoadBMP("images/tank.bmp");
    x=posx;
    y=posy;
    accel=0;  // 0.1 m/c2
    speed=0;
    angle=0;
}
//-----------------------------------------------------------------------------
void XTank::draw()
{
    process();

    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
    SDL_BlitSurface(image, NULL, game->getScreen(), &dest);
}
//-----------------------------------------------------------------------------
int XTank::process()
{
    dT=game->dT;

    speed = speed + ((float)dT*accel);

    speed=speed*0.992;

    //if (speed > max_speed) speed = max_speed;
    //if (speed < (0-max_speed)) speed = (0-max_speed);

    xv = speed*sin(angle*(3.1415926/180));
    yv = speed*cos(angle*(3.1415926/180));

    printf("dT=%d xv=%f yv=%f speed=%f accel=%f angle=%f\n",dT,xv,yv,speed,accel,angle);

    x = x + xv;
    y = y + yv;
}
//-----------------------------------------------------------------------------
int XTank::collisionTest(XObject * pObj)
{
}
//-----------------------------------------------------------------------------
void XTank::speedUp()
{
    float pxPerMs = (accel_speed / 1000)/1000;
    accel=accel+(pxPerMs*(float)dT);
    if (accel>max_accel) accel=max_accel;
}
//-----------------------------------------------------------------------------
void XTank::speedDown()
{
    float pxPerMs = (accel_speed / 1000)/1000;
    accel=accel-(pxPerMs*(float)dT);
    if (accel<(-max_accel)) accel=(0-max_accel);
}
//-----------------------------------------------------------------------------
void XTank::turnToLeft()
{
    angle=angle+((turn_speed*(float)dT)/1000);
}
//-----------------------------------------------------------------------------
void XTank::turnToRight()
{
    angle=angle-((turn_speed*(float)dT)/1000);
}
//-----------------------------------------------------------------------------
void XTank::setNullAccel()
{
    accel=0;
}
//-----------------------------------------------------------------------------
